package com.app.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.app.common.FormStatusTool;
import com.app.mapper.AppealMapper;
import com.app.model.*;
import com.app.service.*;
import com.app.util.CommonHelp;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.FormVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@Controller
@RequestMapping("/api")
public class FormBehindController {
    @Autowired
    private FormService formService;
    @Autowired
    private FormUrlService formUrlService;
    @Autowired
    private ScoresService scoresService;

    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private AppealMapper appealMapper;

    /**
     * 查询投诉列表
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindFormList")
    public Result<Object> getTextList(HttpServletRequest request, FormModel model) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        model.setDepId(user.getDepartmentId());
        FormVo vo = formService.getPageByModel(model);
        if (vo != null) {
            for (int i = 0; i < vo.getList().size(); i++) {
                if (vo.getList().get(i).getStatus() == 2) {
                    vo.getList().get(i).setIsOverdue(2);
                    vo.getList().get(i).setStatus(1);
                    formService.update(vo.getList().get(i));
                }
                if (vo.getList().get(i).getIsOverdue() != null && vo.getList().get(i).getIsOverdue() == 1) {
                    vo.getList().get(i).setIsOverdueText("正常");
                }else if (vo.getList().get(i).getIsOverdue() != null && vo.getList().get(i).getIsOverdue() == 2) {
                    vo.getList().get(i).setIsOverdueText("已逾期");
                }
                LocalDateTime now = LocalDateTime.now();
                if (vo.getList().get(i).getExpirationTime() != null && vo.getList().get(i).getStatus() == 1) {
                    if (now.isAfter(vo.getList().get(i).getExpirationTime())) {
                        vo.getList().get(i).setIsOverdue(2);
                        vo.getList().get(i).setIsOverdueText("已逾期");
                        formService.update(vo.getList().get(i));
                    }
                }
                UserModel userModel = userService.getUserById(vo.getList().get(i).getUserId());
                vo.getList().get(i).setFaceUrl(userModel.getFaceUrl());
                vo.getList().get(i).setUserName(userModel.getUserName());
                vo.getList().get(i).setQuestionTypeText(categoryService.getCategoryById(vo.getList().get(i).getQuestionType()).getName());
                for (int j = 0; j < FormStatusTool.STATUS_VO_LIST.size(); j++) {
                    if (vo.getList().get(i).getStatus().equals(FormStatusTool.STATUS_VO_LIST.get(j).getValue())) {
                        vo.getList().get(i).setStatusText(FormStatusTool.STATUS_VO_LIST.get(j).getName());
                    }
                }

            }
            List<AppealModel> appeal = formService.getAppeal();
            if (appeal != null) {
                for (int i = 0; i < vo.getList().size(); i++) {
                    for (int i1 = 0; i1 < appeal.size(); i1++) {
                        if (vo.getList().get(i).getStatus() == 6) {
                            vo.getList().get(i).setAppeal(1);
                        } else {
                            vo.getList().get(i).setAppeal(2);
                        }
                    }
                }
            }
            for (int i = 0; i < vo.getList().size(); i++) {
                Integer id = vo.getList().get(i).getId();
                FormRemarkModel model1 = formService.selectRemark(id);
                if (model1 != null && vo.getList().get(i).getStatus() == 1) {
                    vo.getList().get(i).setStatus(5);
                    for (int j = 0; j < FormStatusTool.STATUS_VO_LIST.size(); j++) {
                        if (vo.getList().get(i).getStatus().equals(FormStatusTool.STATUS_VO_LIST.get(j).getValue())) {
                            vo.getList().get(i).setStatusText(FormStatusTool.STATUS_VO_LIST.get(j).getName());
                        }
                    }
                    formService.update(vo.getList().get(i));
                }
                if (model1 != null && vo.getList().get(i).getStatus() == 2) {
                    vo.getList().get(i).setStatus(5);
                    for (int j = 0; j < FormStatusTool.STATUS_VO_LIST.size(); j++) {
                        if (vo.getList().get(i).getStatus().equals(FormStatusTool.STATUS_VO_LIST.get(j).getValue())) {
                            vo.getList().get(i).setStatusText(FormStatusTool.STATUS_VO_LIST.get(j).getName());
                        }
                    }
                    formService.update(vo.getList().get(i));
                }
            }

        }
        return Result.success(vo);
    }

    /**
     * 根据ID查询单个
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getBehindFormById")
    public Result<Object> getBehindFormById(HttpServletRequest request, Integer id) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        FormModel model = formService.getFormById(id);
        if (model.getRoad() != null) {
            List<RoadModel> road = formService.get();
            for (int i1 = 0; i1 < road.size(); i1++) {
                if (model.getRoad() != null && Objects.equals(model.getRoad(), road.get(i1).getId())) {
                    model.setRoadText(road.get(i1).getName());
                }
                if (model.getStreet() != null && Objects.equals(model.getStreet(), road.get(i1).getId())) {
                    model.setStreetText(road.get(i1).getName());
                }
            }
        }
        model.setPicList(formUrlService.getUrlByFormId(model.getId(), 1));
        model.setVideoList(formUrlService.getUrlByFormId(model.getId(), 2));
        UserModel userModel = userService.getUserById(model.getUserId());
        model.setFaceUrl(userModel.getFaceUrl());
        model.setUserName(userModel.getUserName());
        model.setQuestionTypeText(categoryService.getCategoryById(model.getQuestionType()).getName());
        List<AppealModel> appeal = formService.getAppeal();
        if (appeal != null) {
            for (int i1 = 0; i1 < appeal.size(); i1++) {
                if (model.getStatus() == 6) {
                    model.setAppeal(1);
                } else {
                    model.setAppeal(2);
                }
            }
        }
        return Result.success(model);
    }

    /**
     * 投诉修改
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/updateForm")
    public Result<Object> updateForm(HttpServletRequest request, Integer id, Integer status) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        FormModel model = formService.getFormById(id);
        FormRemarkModel model1 = formService.selectRemark(model.getId());
        if (model1 == null) {
            return Result.error(405, "派件单位未处理，无法结案或驳回！");
        }
        model.setStatus(status);
        model.setUpTime(CommonHelp.getNowTimestamp());
        formService.update(model);
        return Result.success("操作成功");
    }

    /**
     * 投诉删除
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/deleteForm")
    public Result<Object> deleteForm(HttpServletRequest request, Integer id) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        formService.delete(id);
        return Result.success("操作成功");
    }


    /**
     * 核销积分
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/writeOffFormIntegral")
    public Result<Object> writeOffFormIntegral(HttpServletRequest request, Integer id, Integer integral, Integer type) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        ScoresModel model = scoresService.getScoreByUserId(id);
        Integer result = model.getAfterScore() + integral;
        ScoresModel scoresModel = new ScoresModel();
        scoresModel.setAfterScore(result);
        scoresModel.setId(null);
        scoresModel.setSort(integral);
        scoresModel.setOperateType(type);
        scoresModel.setCrtTime(CommonHelp.getNowTimestamp());
        scoresService.insert(scoresModel);
        return Result.success("操作成功");
    }


    /**
     * 状态列表
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getFormStatus")
    public Result<Object> getFormStatus() {
        return Result.success(FormStatusTool.STATUS_VO_LIST);
    }


    /**
     * 部门列表
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getFormDepartment")
    public Result<Object> getFormDepartment(HttpServletRequest request, Integer id) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        FormModel model = formService.getFormById(id);

        CategoryModel categoryModel = categoryService.getCategoryById(model.getQuestionType());
        String[] str = categoryModel.getResponsibleUnit().split(",");

        List<DepartmentModel> departmentModels = departmentService.getListByIds(str);


        return Result.success(departmentModels);
    }

    /**
     * 派件
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/updateFormDep")
    public Result<Object> updateFormDep(HttpServletRequest request, @RequestBody FormModel model) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        FormModel formModel = formService.getFormById(model.getId());
        formModel.setDepId(model.getDepId());
        formModel.setTermId(model.getTermId());
        formModel.setStatus(1);
        formModel.setIsOverdue(1);
        LocalDateTime now = LocalDateTime.now();
        formModel.setStatusTime(CommonHelp.getNowTimestamp());
        if (model.getTermId() == 2) {
            LocalDateTime threeDaysLater = now.plusDays(3);
            formModel.setExpirationTime(threeDaysLater);
        }
        if (model.getTermId() == 3) {
            LocalDateTime sevenDaysLater = now.plusDays(7);
            formModel.setExpirationTime(sevenDaysLater);
        }
        if (model.getTermId() == 4) {
            LocalDateTime fifteenDaysLater = now.plusDays(15);
            formModel.setExpirationTime(fifteenDaysLater);
        }
        if (model.getTermId() == 5) {
            LocalDateTime thirtyDaysLater = now.plusDays(30);
            formModel.setExpirationTime(thirtyDaysLater);
        }
        formService.update(formModel);
        QueryWrapper<AppealModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("form_id", model.getId());
        queryWrapper.eq("department_id", model.getDepId());
        List<AppealModel> appealModels = appealMapper.selectList(queryWrapper);
        if (!appealModels.isEmpty()) {
            for (AppealModel appealModel : appealModels) {
                appealMapper.deleteById(appealModel.getId());
            }
        }
        return Result.success("操作成功");
    }

    //期限查询
    @GetMapping("/selectTermList")
    public Result<Object> selectTermList() {
        return Result.success(formService.selectTermList());
    }

    //问题类型列表
    @GetMapping("/selectTypeList")
    public Result<Object> selectTypeList() {
        return Result.success(formService.selectTypeList());
    }

    //督察交办列表
    @GetMapping("/getSuperintendList")
    public Result<Object> getSuperintendList(HttpServletRequest request, FormModel model) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        model.setDepId(user.getDepartmentId());
        FormVo vo = formService.getSuperintendList(model);
        if (vo != null) {
            for (int i = 0; i < vo.getList().size(); i++) {
                if (vo.getList().get(i).getStatus() == 2) {
                    vo.getList().get(i).setIsOverdue(2);
                    vo.getList().get(i).setStatus(1);
                    formService.update(vo.getList().get(i));
                }
                if (vo.getList().get(i).getIsOverdue() != null && vo.getList().get(i).getIsOverdue() == 1) {
                    vo.getList().get(i).setIsOverdueText("正常");
                } else if (vo.getList().get(i).getIsOverdue() != null && vo.getList().get(i).getIsOverdue() == 2) {
                    vo.getList().get(i).setIsOverdueText("已逾期");
                }
                LocalDateTime now = LocalDateTime.now();
                if (vo.getList().get(i).getExpirationTime() != null && vo.getList().get(i).getStatus() == 1) {
                    if (now.isAfter(vo.getList().get(i).getExpirationTime())) {
                        vo.getList().get(i).setIsOverdue(2);
                        vo.getList().get(i).setIsOverdueText("已逾期");
                        formService.update(vo.getList().get(i));
                    }
                }
                UserModel userModel = userService.getUserById(vo.getList().get(i).getUserId());
                vo.getList().get(i).setFaceUrl(userModel.getFaceUrl());
                vo.getList().get(i).setUserName(userModel.getUserName());
                vo.getList().get(i).setQuestionTypeText(categoryService.getCategoryById(vo.getList().get(i).getQuestionType()).getName());
                for (int j = 0; j < FormStatusTool.STATUS_VO_LIST.size(); j++) {
                    if (vo.getList().get(i).getStatus().equals(FormStatusTool.STATUS_VO_LIST.get(j).getValue())) {
                        vo.getList().get(i).setStatusText(FormStatusTool.STATUS_VO_LIST.get(j).getName());
                    }
                }
            }
            List<AppealModel> appeal = formService.getAppeal();
            if (appeal != null) {
                for (int i = 0; i < vo.getList().size(); i++) {
                    for (int i1 = 0; i1 < appeal.size(); i1++) {
                        if (vo.getList().get(i).getStatus() == 6) {
                            vo.getList().get(i).setAppeal(1);
                        } else {
                            vo.getList().get(i).setAppeal(2);
                        }
                    }
                }
            }
            for (int i = 0; i < vo.getList().size(); i++) {
                Integer id = vo.getList().get(i).getId();
                FormRemarkModel model1 = formService.selectRemark(id);
                if (model1 != null && vo.getList().get(i).getStatus() == 1) {
                    vo.getList().get(i).setStatus(5);
                    for (int j = 0; j < FormStatusTool.STATUS_VO_LIST.size(); j++) {
                        if (vo.getList().get(i).getStatus().equals(FormStatusTool.STATUS_VO_LIST.get(j).getValue())) {
                            vo.getList().get(i).setStatusText(FormStatusTool.STATUS_VO_LIST.get(j).getName());
                        }
                    }
                    formService.update(vo.getList().get(i));
                }
                if (model1 != null && vo.getList().get(i).getStatus() == 2) {
                    vo.getList().get(i).setStatus(5);
                    for (int j = 0; j < FormStatusTool.STATUS_VO_LIST.size(); j++) {
                        if (vo.getList().get(i).getStatus().equals(FormStatusTool.STATUS_VO_LIST.get(j).getValue())) {
                            vo.getList().get(i).setStatusText(FormStatusTool.STATUS_VO_LIST.get(j).getName());
                        }
                    }
                    formService.update(vo.getList().get(i));
                }
            }
        }
        return Result.success(vo);
    }

    //申诉  /api/insertAppeal  新增  /api/selectAppeal 根据id查询处理
    @PostMapping("/insertAppeal")
    public Result<Object> insertAppeal(@RequestBody AppealModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        Integer adminUserId = user.getId();
        Integer departmentId = departmentService.select(adminUserId);
        model.setDepartmentId(departmentId);
        AppealModel model1 = formService.selectAppealById(model.getFormId());
        if (model.getContent().equals("")) {
            formService.deleteAppeal(model.getFormId());
            FormModel model2 = new FormModel();
            model2.setId(model.getFormId());
            model2.setStatus(1);
            formService.update(model2);
            return Result.success("操作成功！");
        }
        if (model1 != null) {
            FormModel model2 = new FormModel();
            model2.setId(model.getFormId());
            model2.setStatus(6);
            model2.setUpTime(CommonHelp.getNowTimestamp());
            formService.update(model2);
            model.setId(model1.getId());
            model.setUpdateTime(LocalDateTime.now());
            formService.updateAppeal(model);
        } else {
            FormModel model2 = new FormModel();
            model2.setId(model.getFormId());
            model2.setStatus(6);
            model2.setUpTime(CommonHelp.getNowTimestamp());
            formService.update(model2);
            model.setCreateTime(LocalDateTime.now());
            formService.insertAppeal(model);
        }

        return Result.success("操作成功！");
    }

    @GetMapping("/selectAppeal")
    public Result<Object> selectAppeal(Integer id) {
        return Result.success(formService.selectAppeal(id));
    }

    //处理
    @PostMapping("/insertHandle")
    public Result<Object> insertHandle(@RequestBody FormRemarkModel model,
                                       HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.valueOf(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        Integer adminUserId = user.getId();
        Integer departmentId = departmentService.select(adminUserId);
//        formService.insertHandle(content,formId,url,adminUserId);
        model.setDepartmentId(departmentId);
        FormRemarkModel model1 = formService.selectRemark(model.getFormId());
        if (model1 != null) {
            FormModel formById = formService.getFormById(model1.getFormId());
            if (formById != null && formById.getStatus() == 3) {
                return Result.error(406, "已经结案或驳回，无法修改处理结果");
            }
            if (formById != null && formById.getStatus() == 4) {
                return Result.error(406, "已经结案或驳回，无法修改处理结果");
            }
        }
        if (model1 != null) {
            model.setId(model1.getId());
            formService.updateHandle(model);
        } else {
            formService.insertHandle(model);
        }
        return Result.success("操作成功！");
    }

    @GetMapping("/selectHandle")
    public Result<Object> selectHandle(Integer id) {
        return Result.success(formService.selectHandle(id));
    }

    //导出from表
    @PostMapping("/exportExcel")
    @ResponseBody
    public void exportExcel(@RequestBody FormModel model, HttpServletResponse response) throws IOException {
        //道路列表
        List<RoadAndStreetModel> road = formService.getRoad();
        //街道列表
        List<RoadAndStreetModel> street = formService.getStreet();
        //问题类型列表
        List<CategoryModel> categoryModels = formService.selectTypeList();
        //单位列表
        List<DepartmentModel> depModelList = departmentService.getAll();
        //用户列表
        List<UserModel> userModels = userService.selectList();
        List<FormModel> list = formService.selectList(model.getId());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getStatusTime() != null) {
                list.get(i).setStatusTimeText(dateFormat.format(list.get(i).getStatusTime()));
            }
            if (list.get(i).getCrtTime() != null) {
                list.get(i).setCrtTimeText(dateFormat.format(list.get(i).getCrtTime()));
            }
            if (list.get(i).getUpTime() != null) {
                list.get(i).setUpTimeText(dateFormat.format(list.get(i).getUpTime()));
            }
            if (list.get(i).getExpirationTime() != null) {
                list.get(i).setExpirationTimeText(list.get(i).getExpirationTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm")));
            }
            for (int i1 = 0; i1 < road.size(); i1++) {
                if (list.get(i).getRoad() != null && list.get(i).getRoad().equals(road.get(i1).getId())) {
                    list.get(i).setRoadText(road.get(i1).getName());
                }
            }
            for (int i1 = 0; i1 < street.size(); i1++) {
                if (list.get(i).getStreet() != null && list.get(i).getStreet().equals(street.get(i1).getId())) {
                    list.get(i).setStreetText(street.get(i1).getName());
                }
            }
            for (int i1 = 0; i1 < categoryModels.size(); i1++) {
                if (list.get(i).getQuestionType() != null && list.get(i).getQuestionType().equals(categoryModels.get(i1).getId())) {
                    list.get(i).setQuestionTypeText(categoryModels.get(i1).getName());
                }
            }
            for (int i1 = 0; i1 < depModelList.size(); i1++) {
                if (list.get(i).getDepId() != null && list.get(i).getDepId().equals(depModelList.get(i1).getId())) {
                    list.get(i).setDepText(depModelList.get(i1).getName());
                }
            }
            for (int i1 = 0; i1 < userModels.size(); i1++) {
                if (list.get(i).getUserId() != null && list.get(i).getUserId().equals(userModels.get(i1).getId())) {
                    list.get(i).setUserName(userModels.get(i1).getUserName());
                }
            }
            if (list.get(i).getStatus() != null && list.get(i).getStatus() == 0) {
                list.get(i).setStatusText("待派件");
            }
            if (list.get(i).getStatus() != null && list.get(i).getStatus() == 1) {
                list.get(i).setStatusText("处理中");
            }
            if (list.get(i).getStatus() != null && list.get(i).getStatus() == 2) {
                list.get(i).setStatusText("已逾期");
            }
            if (list.get(i).getStatus() != null && list.get(i).getStatus() == 3) {
                list.get(i).setStatusText("结案");
            }
            if (list.get(i).getStatus() != null && list.get(i).getStatus() == 4) {
                list.get(i).setStatusText("驳回");
            }
            if (list.get(i).getStatus() != null && list.get(i).getStatus() == 5) {
                list.get(i).setStatusText("已整改");
            }
            if (list.get(i).getStatus() != null && list.get(i).getStatus() == 6) {
                list.get(i).setStatusText("已申诉");
            }
            if (list.get(i).getTermId() != null && list.get(i).getTermId() == 2) {
                list.get(i).setTermText("三天");
            }
            if (list.get(i).getTermId() != null && list.get(i).getTermId() == 3) {
                list.get(i).setTermText("七天");
            }
            if (list.get(i).getTermId() != null && list.get(i).getTermId() == 4) {
                list.get(i).setTermText("十五天");
            }
            if (list.get(i).getTermId() != null && list.get(i).getTermId() == 5) {
                list.get(i).setTermText("三十天");
            }

        }
        Map<String, Object> map = Maps.newHashMap();
        map.put("list", list);
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy_MM_dd_HH_mm"));
        String fileName = null;
        if (model.getId() == 1) {
            fileName = "随手拍" + time;
        }
        if (model.getId() == 2) {
            fileName = "督察交办" + time;
        }
        try {
            TemplateExportParams params = new TemplateExportParams("D:/excel/form.xlsx", true);
            exportExcel(params, map, fileName, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void exportExcel(TemplateExportParams params, Map<String, Object> map, String fileName, HttpServletResponse response) throws IOException {

        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        downLoadExcel(fileName, response, workbook);
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
            ServletOutputStream outStream = null;
            try {
                outStream = response.getOutputStream();
                workbook.write(outStream);

            } finally {
                outStream.flush();
                outStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
